package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import java.util.Collection; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import java.util.List;

import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;
import com.lb.jwitter.service.TblQLDTTXALop;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
import com.lb.jwitter.service.TblQLDTTXALopService;

import com.lb.jwitter.service.TblQLDTTXADMNganh;
import com.lb.jwitter.service.TblQLDTTXADMNganhService;

import com.lb.jwitter.service.TblQLDTTXADMKhoa;
import com.lb.jwitter.service.TblQLDTTXADMKhoaService;

import com.lb.jwitter.service.TblQLDTTXADMKhoaHoc;
import com.lb.jwitter.service.TblQLDTTXADMKhoaHocService;

import com.lb.jwitter.service.TblQLDTTXADMChiNhanh;
import com.lb.jwitter.service.TblQLDTTXADMChiNhanhService;

import com.lb.jwitter.service.TblQLDTTXAHoSoXetTuyen;
import com.lb.jwitter.service.TblQLDTTXAHoSoXetTuyenService;

import com.lb.jwitter.service.TblQLDTTXADMTrangThaiHocVien;
import com.lb.jwitter.service.TblQLDTTXADMTrangThaiHocVienService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienUpdateAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private int id; 
    public int getId() {
       return id;
 }

    public void setId(int id) {
       this.id = id;
 }

    private String mSHV; 
    public void setMSHV(String mSHV) {
       this.mSHV = mSHV;
 }

    public String getMSHV() {
       return mSHV;
 }

    private String hoTen; 
    public void setHoTen(String hoTen) {
       this.hoTen = hoTen;
 }

    public String getHoTen() {
       return hoTen;
 }
    
    private String ten; 
    public void setTen(String ten) {
       this.ten = ten;
 }

    public String getTen() {
       return ten;
 }

    private String hinhAnh; 
    public void setHinhAnh(String hinhAnh) {
       this.hinhAnh = hinhAnh;
 }

    public String getHinhAnh() {
       return hinhAnh;
 }

    private int lopId; 
    public void setLopId(int lopId) {
       this.lopId = lopId;
 }

    public int getLopId() {
       return lopId;
 }

    private int nganhId; 
    public void setNganhId(int nganhId) {
       this.nganhId = nganhId;
 }

    public int getNganhId() {
       return nganhId;
 }

    private int khoaId; 
    public void setKhoaId(int khoaId) {
       this.khoaId = khoaId;
 }

    public int getKhoaId() {
       return khoaId;
 }

    private int khoaHocId; 
    public void setKhoaHocId(int khoaHocId) {
       this.khoaHocId = khoaHocId;
 }

    public int getKhoaHocId() {
       return khoaHocId;
 }

    private int chiNhanhId; 
    public void setChiNhanhId(int chiNhanhId) {
       this.chiNhanhId = chiNhanhId;
 }

    public int getChiNhanhId() {
       return chiNhanhId;
 }

    private int hoSoId; 
    public void setHoSoId(int hoSoId) {
       this.hoSoId = hoSoId;
 }

    public int getHoSoId() {
       return hoSoId;
 }

    private String ngayLap; 
    public void setNgayLap(String ngayLap) {
       this.ngayLap = ngayLap;
 }

    public String getNgayLap() {
       return ngayLap;
 }

    private String ghiChu; 
    public void setGhiChu(String ghiChu) {
       this.ghiChu = ghiChu;
 }

    public String getGhiChu() {
       return ghiChu;
 }

    private int trangThaiId; 
    public void setTrangThaiId(int trangThaiId) {
       this.trangThaiId = trangThaiId;
 }

    public int getTrangThaiId() {
       return trangThaiId;
 }

    private TblQLDTTXAHocVien tblQLDTTXAHocVien; 
    public void setTblQLDTTXAHocVien (TblQLDTTXAHocVien tblQLDTTXAHocVien) {
        this.tblQLDTTXAHocVien = tblQLDTTXAHocVien;
 }

    private Collection<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public void setTblQLDTTXAHocViens(Collection<TblQLDTTXAHocVien> tblQLDTTXAHocViens) {
        this.tblQLDTTXAHocViens = tblQLDTTXAHocViens; 
      } 

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private TblQLDTTXALopService tblQLDTTXALopService; 

    private TblQLDTTXADMNganhService tblQLDTTXADMNganhService; 

    private TblQLDTTXADMKhoaService tblQLDTTXADMKhoaService; 

    private TblQLDTTXADMKhoaHocService tblQLDTTXADMKhoaHocService; 

    private TblQLDTTXADMChiNhanhService tblQLDTTXADMChiNhanhService; 

    private TblQLDTTXAHoSoXetTuyenService tblQLDTTXAHoSoXetTuyenService; 

    private TblQLDTTXADMTrangThaiHocVienService tblQLDTTXADMTrangThaiHocVienService; 
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 
    

	private int preUpdate(Message msg, TblQLDTTXAHocVien obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblQLDTTXAHocVien obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
   { 
    try{ 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien", "sua")){ 
        return "noprivilege"; 
    }
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 
        tblQLDTTXALopService = (TblQLDTTXALopService) context.getBean("tbl_QLDTTXA_LopClient"); 

        tblQLDTTXADMNganhService = (TblQLDTTXADMNganhService) context.getBean("tbl_QLDTTXA_DM_NganhClient"); 

        tblQLDTTXADMKhoaService = (TblQLDTTXADMKhoaService) context.getBean("tbl_QLDTTXA_DM_KhoaClient"); 

        tblQLDTTXADMKhoaHocService = (TblQLDTTXADMKhoaHocService) context.getBean("tbl_QLDTTXA_DM_KhoaHocClient"); 

        tblQLDTTXADMChiNhanhService = (TblQLDTTXADMChiNhanhService) context.getBean("tbl_QLDTTXA_DM_ChiNhanhClient"); 

        tblQLDTTXAHoSoXetTuyenService = (TblQLDTTXAHoSoXetTuyenService) context.getBean("tbl_QLDTTXA_HoSoXetTuyenClient"); 

        tblQLDTTXADMTrangThaiHocVienService = (TblQLDTTXADMTrangThaiHocVienService) context.getBean("tbl_QLDTTXA_DM_TrangThaiHocVienClient");
        
        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        
        msg = new Message(); 
        tblQLDTTXAHocVien = tblQLDTTXAHocVienService.gettblQLDTTXAHocVien(new Long(id)); 
        tblQLDTTXAHocVien.setMSHV(mSHV); 
        tblQLDTTXAHocVien.setHoTen(hoTen.toUpperCase()); 
        tblQLDTTXAHocVien.setTen(ten.toUpperCase()); 
        tblQLDTTXAHocVien.setHinhAnh(hinhAnh); 
        tblQLDTTXAHocVien.setLopId(tblQLDTTXALopService.gettblQLDTTXALop(new Long(lopId))); 
        tblQLDTTXAHocVien.setNganhId(tblQLDTTXADMNganhService.gettblQLDTTXADMNganh(new Long(nganhId))); 
        tblQLDTTXAHocVien.setKhoaId(tblQLDTTXADMKhoaService.gettblQLDTTXADMKhoa(new Long(khoaId))); 
        tblQLDTTXAHocVien.setKhoaHocId(tblQLDTTXADMKhoaHocService.gettblQLDTTXADMKhoaHoc(new Long(khoaHocId))); 
        tblQLDTTXAHocVien.setChiNhanhId(tblQLDTTXADMChiNhanhService.gettblQLDTTXADMChiNhanh(new Long(chiNhanhId))); 
        tblQLDTTXAHocVien.setHoSoId(tblQLDTTXAHoSoXetTuyenService.gettblQLDTTXAHoSoXetTuyen(new Long(hoSoId))); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayLap); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblQLDTTXAHocVien.setNgayLap(d); 
        tblQLDTTXAHocVien.setGhiChu(ghiChu); 
        
        TblQLDTTXADMTrangThaiHocVien trangThaiMoi = tblQLDTTXADMTrangThaiHocVienService.gettblQLDTTXADMTrangThaiHocVien(new Long(trangThaiId));
        TblQLDTTXADMTrangThaiHocVien trangThaiCu = tblQLDTTXAHocVien.getTrangThaiId();
        //DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        

        tblQLDTTXAHocVien.setTrangThaiId(trangThaiMoi); 
        preUpdate(msg,tblQLDTTXAHocVien); 
       if (msg.isPreMessage_b()) 
       { 
           tblQLDTTXAHocVienService.updatetblQLDTTXAHocVien(tblQLDTTXAHocVien); 
           postUpdate(msg, tblQLDTTXAHocVien); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_QLDTTXA_HocVien Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
